package com.bike.controller.information;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.bike.controller.BaseController;
import com.bike.entity.HhCity;
import com.bike.service.impl.HhCityServiceImpl;
import com.bike.util.Constant;
import com.bike.util.PageData;
import com.bike.util.TimeManager;
import com.topit.show.aliapi.AlyOssFileUpLoadUtils;

 
@Controller
@RequestMapping(value="/image")
public class HhImageController extends BaseController {
	
		@Autowired
		private HhCityServiceImpl  hhCityService;
	
	 
		/**
		 * 列表查询(或根据条件)
		 */
		@RequestMapping("upload")
		@ResponseBody
		public PageData list(@RequestParam(required=false) MultipartFile file,HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			PageData result=getSuccessPd();
			
			String img="";
			int length=0;
			try {
				if(file!=null){
					length=file.getBytes().length;
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			if(length>0&&length<10000000){
				String path=pd.getString("path");
				String fold=path.concat("/").concat(TimeManager.getInstance().getYMD());
				String fileName=file.getOriginalFilename();
				String name=UUID.randomUUID().toString().concat(fileName.substring(fileName.lastIndexOf(".")));
				String folder=Constant.filePath.concat(fold);
				 img=fold.concat("/").concat(name);
				File upde=new File(folder);
				if(!upde.exists()){
					upde.mkdirs();
				}
				File upfile=new File(upde,name);
				try {
					file.transferTo(upfile);
					new AlyOssFileUpLoadUtils().fileUpload(img, upfile);
				} catch (IllegalStateException | IOException e) {
					e.printStackTrace();
				}
			}
		 
			result.put("url", img);
			return result;
		}
		 
}
